<?php
$page = 'admin';
include '../database/connect.php';
$quiz_id = $_GET['quiz_id'];
if (isset($_GET['save'])) {
    session_start();
    extract($_POST);
    $added_by = $_SESSION['username'];
    $query = "INSERT INTO questions SET quiz_id='$quiz_id', content='$content',marks='$marks',  added_by='$added_by'";
    mysql_query($query) or die("Error executing '$query':" . mysql_error());
    header("Location: ?quiz_id=$quiz_id");
}

if (isset($_GET['update'])) {
    extract($_GET);
    $query = "UPDATE questions SET content='$content',marks='$marks' WHERE question_id='$question_id'";
    mysql_query($query) or die("Error executing '$query':" . mysql_error());
    header("Location: ?quiz_id=$quiz_id");
}

if (isset($_GET['delete'])) {
    extract($_GET);
    $query = "DELETE FROM questions WHERE question_id='$question_id'";
    mysql_query($query) or die("Error executing '$query':" . mysql_error());
    header("Location: ?quiz_id=$quiz_id");
}


$query = "select * from quizes where quiz_id='$quiz_id'";
$result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
$row = mysql_fetch_assoc($result);
$course_id = $row['course_id'];
$quiz = $row['description'];

//lets get the course name for purpose of navigation
$query = "SELECT * FROM courses where id='$course_id'";
$result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
$row = mysql_fetch_assoc($result);
$course = $row['course'];

require_once 'header.php';
?>
<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("#quiz_id_form").validationEngine();
    });


    function editQues(tid,content,marks){
        content=prompt("Edit content",content);
        if(content.length>0){
            marks=prompt("Edit marks",marks);
            if(marks.length>0){
                location="?update=1&question_id="+tid+"&marks="+marks+"&content="+content+"&quiz_id=<?php echo $quiz_id ?>";
            }
        }
    }



    function deleteQues(tid){
        if(confirm("Are you sure you want to delete?")){
            window.location="?delete=1&question_id="+tid+"&quiz_id=<?php echo $quiz_id ?>";
        }
    }
</script>



<p>
    <a href="admin_home.php">Admin home</a>&gt;&gt;<a href='courses.php'>Courses</a>&gt;&gt;
    <a href="lessons.php?course_id=<?php echo $course_id; ?>"><?php echo $course; ?></a>&gt;&gt;
    <?php echo $quiz; ?>
</p>

<form action="?save=1" method="POST" id="questions_form">
    <input type="hidden" name="quiz_id" value="<?php echo $_GET['quiz_id']; ?>" />
    <table class="table">
        <tr style="text-align: left"><th>Question</th><th>Marks</th></tr>
        <tr>
            <td><textarea style="width: 90%" class="validate[required] wide" name="content" id='content'></textarea></td>
            <td><input type="text" name="marks" size="2" /> </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: right;"><input type="submit" value="Add Question"/></td>
        </tr>
    </table>
</form>
<?php
    $username = $_SESSION['username'];
    $query = "SELECT * FROM questions where quiz_id='$quiz_id' order by date_added asc";
    $count = 0;
    $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
    if (mysql_num_rows($result) == 0) {
//    echo "No in the database";
    } else {
?>

        <table class="table" style="text-align: left">
            <tr>
                <th >

                </th>
                <th >
                    Question
                </th>
                <th >
                    Marks
                </th>
        <!--            <th >
                    Edit
                </th>

                <th >Delete
                </th>-->
            </tr>
    <?php
        while ($row = mysql_fetch_assoc($result)) {
            //var_dump($row);
            extract($row);
            $count++;
    ?>
            <tr>
                <td >
            <?php echo $count; ?>
        </td>
        <td >
            <a href="answers.php?question_id=<?php echo $question_id; ?>"><?php echo $content; ?></a>
        </td>
        <td >
            <?php echo $marks; ?>
        </td>
        <td >
            <a href="javascript:editQues(<?php echo $question_id ?>,'<?php echo $content; ?>', <?php echo $marks; ?>);"><img src="../images//edit.png" alt="Edit" title="Edit"/></a>
        </td><td>
            <a href="javascript:deleteQues(<?php echo $question_id ?>);"><img src="../images//delete.png" alt="Delete" title="Delete"/></a>
        </td>>
    </tr>
    <?php
        }
    }
    ?>
</table>


<?php require_once('footer.php'); ?>